/**
 * Created by yindume on 2018/12/10.
 */
var express = require('express');
var router = express.Router();
var db = require('../../../libs/mysql');
//统一返回格式
var res_Data;
router.use( function(req, res, next) {
    res_Data = {
        code: 0,
        message: ''
    }
    next();
});
router.get('/',function (req,res) {
    var id = req.query.id || '';
    db.query(`SELECT * FROM category`,(err,data)=>{
        if(err){
            res.send('修改数据分类显示').end();
        }else {
            var categorydata = data;
            db.query(`SELECT u.nickname,c.name,t.* FROM contents t LEFT JOIN user u ON t.userid = u.id LEFT JOIN category c ON t.categoryid = c.id where t.id=${id}`,(err,data)=>{
                if(err){
                    res.send('修改数据显示').end();
                }else {

                    res.render('admin/content/edit', {
                        userInfo:req.userInfo,
                        categorydata:categorydata,
                        contentedit:data[0]
                    });
                }
            })
        }
    })
});
router.post('/',(req,res)=>{
    var id = req.body.contentid;
    var categoryid = req.body.category;//分类
    var title = req.body.title;//标题
    var abstract = req.body.abstract;//简介
    var content = req.body.editorValue;//内容

    db.query(`UPDATE contents SET categoryid = '${categoryid}',title='${title}',abstract='${abstract}',content='${content}' WHERE id = ${id}`,(err,data)=>{
        if(err){
            res.send('修改数据提交').end();
        }else {
            res_Data.message = '修改成功';
            res.json(res_Data);
            return;
        }
    })
})



module.exports = router;